const { FileFolder, Sheet, User } = require('../model')

exports.list = async (req, res, next) => {
    try {
        const { pageSize = 20, pageNum = 0, keyword, type, userId } = req.body;
        if (!userId) {
            return res.status(400).json({
                data: '请传入userId'
            })
        }
        let query = { author: userId };
        if (keyword) {
            query.title = { $regex: keyword, $options: 'i' };
        }
        let sheetModel = await FileFolder.findOne(query)
        let data = null
        switch (type) {
            case "all":
                data = sheetModel.allSheet.slice((pageNum - 1) * pageSize, pageSize)
                break;
            case "recent":
                data = sheetModel.recentSheet
                break;
            case "starred":
                data = sheetModel.starredSheet.slice((pageNum - 1) * pageSize, pageSize)
                break;
            case "shared":
                data = sheetModel.sharedSheet.slice((pageNum - 1) * pageSize, pageSize)
                break;
        }
        const promises = data.map(async (item) => {
            // 获取当前时间戳（单位：毫秒）
            const currentTime = Date.now();
            // 计算三天前的时间戳（单位：毫秒）
            const threeDaysAgoTime = currentTime - 3 * 24 * 60 * 60 * 1000;
            let result = await Sheet.findOne({ _id: item });
            if (type == 'recent' && result.updatedAt >= threeDaysAgoTime) {
                let author = await User.findOne({ _id: result.author })
                result.author = JSON.stringify({ username: author.username, email: author.email, avatar: author.avatar, _id: author._id })
                return result
            } else {
                let author = await User.findOne({ _id: result.author })
                result.author = JSON.stringify({ username: author.username, email: author.email, avatar: author.avatar, _id: author._id })
                return result
            }
        })
        let results = await Promise.all(promises);
        results = results.filter(function (s) {
            return s != null && s
        })
        if (type == 'recent') {
            results.slice((pageNum - 1) * pageSize, pageSize)
        }
        results.sort(function (a, b) {
            return b.updatedAt - a.updatedAt
        })
        res.status(200).json({
            code: 200,
            data: results
        })
    } catch (err) {
        console.log(err);

        next(err);
    }
};

exports.getNum = async (req, res, next) => {
    try {
        const { userId } = req.query;
        if (!userId) {
            return res.status(400).json({
                data: '请传入userId'
            })
        }
        let sheetModel = await FileFolder.findOne({ author: userId })
        const promises = sheetModel.recentSheet.map(async (item) => {
            // 获取当前时间戳（单位：毫秒）
            const currentTime = Date.now();
            // 计算三天前的时间戳（单位：毫秒）
            const threeDaysAgoTime = currentTime - 3 * 24 * 60 * 60 * 1000;
            let result = await Sheet.findOne({ _id: item });
            let author = await User.findOne({ _id: result.author })
            result.author = JSON.stringify({ username: author.username, email: author.email, avatar: author.avatar, _id: author._id })
            return result
        })
        let allNum = sheetModel.allSheet.length
        let recentNum = promises.length
        let starredNum = sheetModel.starredSheet.length
        let sharedNum = sheetModel.sharedSheet.length
        res.status(200).json({
            code: 200,
            data: {
                allNum,
                recentNum,
                starredNum,
                sharedNum
            }
        })
    } catch (err) {
        console.log(err);
    }
}